<?php

namespace app\agent\controller;

use app\models\{Region, User as Users, Coupon, Address, AgentArea, Agent, IntergralLog, Product,Order};
use think\Db;
use think\Validate;

class User extends Base
{

    public function index()
    {
        if ($this->request->isAjax()) {
            //获取合伙人身份
            // $city = (new AgentArea())->distinct(true)->where(['type' => 2, 'agentid' => $this->request->agent['id']])->column('city');
            // $area = (new AgentArea())->distinct(true)->where(['type' => 3, 'agentid' => $this->request->agent['id']])->column('area');
            // $street = (new AgentArea())->distinct(true)->where(['type' => 4, 'agentid' => $this->request->agent['id']])->column('street');
            // $user_id = (new Address())->distinct(true)
            //     ->whereOr(function ($query) use ($city) {
            //         $query->where(['city' => $city]);
            //     })->whereOr(function ($query) use ($area) {
            //         $query->where(['area' => $area]);
            //     })->whereOr(function ($query) use ($street) {
            //         $query->where(['street' => $street]);
            //     })->column('user_id');
            $user_id = (new Order())->where('agentid',$this->request->agent['id'])->where(['status' => [4]])->column('user_id');
            $model = new Users();
            $post = input();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $where = array();
            if (!empty($post['title'])) {
                $where[] = ['nickName|mobile', 'like', '%' . $post['title'] . '%'];
            }
            $where[] = ['id', 'in', $user_id];
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("id desc")->limit($limit)->page($page)->select();
            foreach ($list as $result) {
                $address = (new Address())->where('user_id', $result['id'])->order('is_default desc')->find() ?? [];
                if (!empty($address)) {
                    $address_name = getdrees($address['street'], 2);
                    $address['province_name'] = $address_name['province'];
                    $address['city_name'] = $address_name['city'];
                    $address['area_name'] = $address_name['area'];
                    $address['street_name'] = $address_name['street'];
                }
                $result['address'] = $address;
            }
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            return view('', []);
        }
    }


    public function interlog()
    {
        if ($this->request->isAjax()) {
            $model = new IntergralLog();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $user_id = input('id/d') ?? 0;
            $where = array();
            $where[] = ['user_id', '=', $user_id];
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("id desc")->limit($limit)->page($page)->select();
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            return view('', []);
        }
    }


    public function coupon()
    {
        if ($this->request->isAjax()) {
            $model = new Coupon();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $user_id = input('user_id/d', 0);
            $promo = input('promo/d', 0);
            $where = array();
            //$where[] = ['type', '=', 2];
            $where[] = ['agentid', 'in', [0,$this->request->agent['id']]];
            if (!empty($user_id)) {
                $where[] = ['user_id', '=', $user_id];
            }
            if (!empty($promo)) {
                $where[] = ['promo', '=', $promo];
                $where[] = ['system', '=', 3];
            }
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("endtime asc")->limit($limit)->page($page)->select();
            foreach ($list as $result) {
                $result['product_name'] = '--';
                if ($result['types'] == 2) {
                    $result['product_name'] = (new Product())->where('id', $result['productid'])->value('title');
                }
                $result['usernick'] = (new Users())->where('id', $result['user_id'])->value('nickName');
            }
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            return view('', []);
        }
    }

    public function couponel()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id/d', 0);
            if (empty($id)) {
                return json(['code' => 1, 'msg' => '请选择删除项目']);
            }
            $banner = (new Coupon())->where(['id' => $id, 'agentid' => $this->request->agent['id']])->find();
            if (empty($banner)) {
                return json(['code' => 1, 'msg' => '优惠券不存在或无权限']);
            }
            if ($banner['status'] != 1) {
                return json(['code' => 1, 'msg' => '优惠券已作废']);
            }
            $res = (new Coupon())->where('id', $id)->update(['status' => 2, 'remark' => $banner['remark'] . '|代理员作废']);
            if ($res) {
                return json(['code' => 200, 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'msg' => '操作失败']);
        }
    }

    public function sendcoupon()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'getmoney' => 'require|gt:0',
                'money' => 'require|gt:0',
                'user_id' => 'require',
                'days' => 'require|gt:0',
            ], [
                'getmoney.require' => '可用金额不能为空',
                'getmoney.gt' => '可用金额必须大于0',
                'money.require' => '抵扣金额不能为空',
                'money.gt' => '抵扣金额必须大于0',
                'user_id.require' => '用户ID不能为空',
                'days.require' => '有效期不能为空',
                'days.gt' => '有效期必须大于0',

            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'msg' =>  $validate->getError()]);
            }
            $data['type'] = 2;
            $data['agentid'] = $this->request->agent['id'];
            // if ($data['getmoney'] < $data['money']) {
            //     return json(['code' => 1, 'msg' => '抵扣金额不能超过可用金额']);
            // }
            $data['endtime'] = date('Y-m-d H:i:s', time() + $data['days'] * 86400);
            $res = (new Coupon())->allowfield(true)->save($data);
            if ($res) {
                return json(['code' => 200, 'msg' => '编辑成功']);
            }
            return json(['code' => 200, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('user_id/d', 0);
            return view('', [
                'id' => $id
            ]);
        }
    }
}
